<style lang="less">
    @import "detail.less";
</style>
<template>


    <div>
        <Card style="width:100%" shadow>
            <Collapse v-model="showPanel">
                <Panel name="1">
                    日志
                    <div slot="content">
                        <approveHistory ref="approveHistory" :Obj="approveHistoryObj"/>
                    </div>
                </Panel>
                <Panel name="2">
                    基本信息
                    <div slot="content">
                        <Form ref="baseInfo" :model="caseInfo" class="detailForm"
                              :label-width="100"
                              inline>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="startTime" label="申请时间">
                                        <DatePicker type="date" v-model="caseInfo.startTime" disabled placeholder="选择日期"></DatePicker>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="endTime" label="结束时间" >
                                        <DatePicker type="date" v-model="caseInfo.endTime" disabled placeholder="选择日期"></DatePicker>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="createBy" label="创建人">
                                        <Input type="text" v-model="caseInfo.createTagStr" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="caseNo" label="案件编号" >
                                        <Input type="text" v-model="caseInfo.caseNo" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </div>
                </Panel>
                <Panel name="3">
                    合同信息
                    <div slot="content">
                        <Form ref="contractInfo" :model="contractInfo" class="detailForm"
                              :label-width="100"
                              inline>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="applyNo" label="申请编号" >
                                        <Input type="text" v-model="contractInfo.applyNo" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="custName" label="客户姓名" >
                                        <Input type="text" v-model="contractInfo.custName" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="certNo" label="证件号码" >
                                        <Input type="text" v-model="contractInfo.certNo" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="phone" label="联系方式" >
                                        <Input type="text" v-model="contractInfo.phone" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="startDate" label="合同激活时间" >
                                        <DatePicker type="date" v-model="contractInfo.startDate" disabled placeholder="选择日期"></DatePicker>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="endDate" label="合同终止时间" >
                                        <DatePicker type="date" v-model="contractInfo.endDate" disabled placeholder="选择日期"></DatePicker>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="contractStatus" label="合同状态" >
                                        <Select v-model="contractInfo.contractStatus" disabled placeholder="请选择">
                                            <Option
                                                v-for="(item,index) in dataDict.contractStatusDict"
                                                :key="index"
                                                :value="item.value"
                                            >{{item.title}}</Option>
                                        </Select>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="channelType" label="渠道归属" >
                                        <Select v-model="contractInfo.channelType" disabled placeholder="请选择">
                                            <Option
                                                v-for="(item,index) in dataDict.channelTypeDict"
                                                :key="index"
                                                :value="item.value"
                                            >{{item.title}}</Option>
                                        </Select>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="overdueDaysHis" label="合同号" >
                                        <Input type="text" v-model="contractInfo.contractNo" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="paidTerms" label="已还期数" >
                                        <Input type="text" v-model="contractInfo.paidTerms" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="unpaidTerms" label="未还期数" >
                                        <Input type="text" v-model="contractInfo.unpaidTerms" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="channelName" label="合作商" >
                                        <Input type="text" v-model="contractInfo.channelName" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="paidTerms" label="月供金额" >
                                        <Input type="text" v-model="contractInfo.monthPayAmt" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="unpaidTerms" label="当期总应收" >
                                        <Input type="text" v-model="contractInfo.currentReceivable" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="channelName" label="上次付款时间" >
                                        <DatePicker type="date" v-model="contractInfo.lastPayTime" disabled placeholder="选择日期"></DatePicker>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="channelName" label="利率类型" >
                                        <Select v-model="contractInfo.rateType" disabled placeholder="请选择">
                                            <Option
                                                v-for="(item,index) in dataDict.rateTypeDict"
                                                :key="index"
                                                :value="item.value"
                                            >{{item.title}}</Option>
                                        </Select>
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </div>
                </Panel>
                <Panel name="4" v-show="this.obj.caseType == 'BEFORE_CHARGE'">
                    提前还款信息
                    <div slot="content">
                        <Form ref="prepaymentInfo" :model="prepaymentInfo" class="detailForm"
                              :label-width="100"
                              inline>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="caseNo" label="案件编号" >
                                        <Input type="text" v-model="prepaymentInfo.caseNo" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="receivableAmount" label="应收净额" >
                                        <Input type="text" v-model="prepaymentInfo.receivableAmount" disabled placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="repayDate" label="结算日期" >
                                        <DatePicker type="date" v-model="prepaymentInfo.repayDate" disabled placeholder="选择日期"></DatePicker>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="8" v-show="this.obj.caseType == 'BEFORE_CHARGE' && this.caseInfo.caseStatus && (this.caseInfo.caseStatus != 'NOT_SUBMIT' && this.caseInfo.caseStatus != 'UNDER_REVIEW')">
                                    <FormItem prop="debitOrderStatus" label="扣款指令" >
                                        <Select v-model="caseInfo.debitOrderStatus" disabled placeholder="请选择">
                                            <Option
                                                v-for="(item,index) in dataDict.validOrInvalidDict"
                                                :key="index"
                                                :value="item.value"
                                            >{{item.title}}</Option>
                                        </Select>
                                    </FormItem>
                                </Col>
                                <Col span="8" v-show="this.obj.caseType == 'BEFORE_CHARGE' && (!this.caseInfo.caseStatus || this.caseInfo.caseStatus == 'NOT_SUBMIT' || this.caseInfo.caseStatus == 'UNDER_REVIEW')">
                                    <FormItem prop="debitOrderStatus" label="扣款指令" >
                                        <Select v-model="prepaymentInfo.debitOrderStatus" disabled placeholder="请选择">
                                            <Option
                                                v-for="(item,index) in dataDict.validOrInvalidDict"
                                                :key="index"
                                                :value="item.value"
                                            >{{item.title}}</Option>
                                        </Select>
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </div>
                </Panel>
                <Panel name="5">
                    业务办理：{{this.obj.caseType == 'BEFORE_CHARGE'?'提前还款':'月供'}}
                    <div slot="content">
                        <Form ref="caseInfo" :model="caseInfo" :rules="caseInfoFormValidate" class="detailForm"
                              :label-width="100"
                              inline>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="receivingBank" label="收款银行">
                                        <Select v-model="caseInfo.receivingBank" placeholder="请选择">
                                            <Option
                                                v-for="(item,index) in dataDict.bankCodeDict"
                                                :key="index"
                                                :value="item.value"
                                            >{{item.title}}</Option>
                                        </Select>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="payAmt" label="打款金额" >
                                        <Input type="text" v-model="caseInfo.payAmt" placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                                <Col span="8">
                                    <FormItem prop="payTime" label="打款时间">
                                        <DatePicker type="date" v-model="caseInfo.payTime" :options="dateLimit" :start-date="dateFocus" placeholder="选择日期"></DatePicker>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="8">
                                    <FormItem prop="payer" label="打款人" >
                                        <Input type="text" v-model="caseInfo.payer" placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span="24">
                                    <FormItem prop="remarks" label="说明">
                                        <Input type="text" class="holeLine" v-model="caseInfo.remarks" placeholder="请输入">
                                        </Input>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Col align="middle">
                                <Form-item>
                                    <Button @click="openUploadModal" type="primary" icon="ios-cloud-upload" >上传附件</Button>
                                    <Button @click="saveHandle" type="primary" icon="md-bookmark">保存</Button>
                                    <Button @click="submitHandle" type="primary" icon="md-send">提交</Button>
                                </Form-item>
                            </Col>
                        </Form>
                    </div>
                </Panel>
            </Collapse>
            <UploadFileModal
                ref="uploadFileModal"
                :modal="isUploadFileModalOpen"
                @closeModal="closeModal"
                :param="fileParams"
                @asyncSaveFileFn="asyncSaveFileFn"
                :isPreview="false"
            />
        </Card>
    </div>
</template>

<script>
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    import {
        queryCorporateByCaseNo,
        saveCorporate,
        submitCorporate
    } from "@/projects/basic/api/contract/corporate/corporateTransferManagement";

    import {getApplyInfoById} from "@/projects/basic/api/contract/clearance/stay-settle";
    import {
        contractInfoForCorporate,
    } from "@/projects/basic/api/contract/common/contract-info/contract-info";
    import {checkFile} from "_p/basic/api/image/image-upload";
    import {isTwoDicmal} from "@/projects/basic/assets/contract/js/utils";

    import approveHistory from "../common/approve-history/approve-history";
    import LeaseFileUpload from '_c/file-upload/index'
    import UploadFileModal from "_p/basic/pages/contract/common/uploadFileModal/uploadFileModal";
    import contractCurrentUser from "_p/basic/pages/contract/common/current-user/contractCurrentUser";
    import {mapGetters} from "vuex";

    export default {
        name: "detailForBusinessEdit",
        components: {
            approveHistory,
            UploadFileModal,
            LeaseFileUpload,
            contractCurrentUser
        },
        props: {
            obj: {
                caseNo: "",
                dataSource: "",
                contractNo: "",
                id:"",
                caseType:"",
                prepaymentApplyId:"",
                type:Object
            }
        },
        computed: {
            ...mapGetters({userInfo: "userInfo", roleType: "roles"})
        },
        data: function () {
            const isMoneyValue = (rule, value, callback) => {
                if (value) {
                    if (!isTwoDicmal(value) && value !== '') {
                        callback(new Error("请输入正确金额"))
                    } else {
                        if(value<=0){
                            callback(new Error("金额必须大于0"))
                        }else{
                            callback()
                        }
                    }
                } else {
                    callback(new Error("不能为空"))
                }
            };
            return {
                dateLimit:{
                    disabledDate (date) {
                        return date && date.valueOf() > Date.now();
                    }
                },
                dateFocus: new Date(),
                contractForm: {},
                contractFormValidate: {},
                isUploadFileModalOpen:false,
                fileParams:{},
                showPanel:[],

                approveHistoryObj:{
                    caseNo:"",
                    contractNo:""
                },
                dicKeys: [
                    "bankCode","businessSource","contractStatus","channelType","caseType","rateType","validOrInvalid"
                ],
                dataDict:{},
                caseInfo:{},
                caseInfoFormValidate:{
                    receivingBank:[{required: true, message: "不能为空", trigger: "change"}],
                    payAmt:[{validator: isMoneyValue, trigger: "blur"}],
                    payTime:[{required: true, message: "不能为空", type:"date", trigger: "blur"}],
                    payer:[{required: true, message: "不能为空", trigger: "blur"}]
                },
                contractInfo:{},
                prepaymentInfo:{},
                contractInfoFormValidate:{},
                productInfo:{},
                bankArriveColums:[
                    {title: "网银编号",key: "ebankNo",minWidth: 160,align: "center"},
                    {title: "到账时间",key: "arrivalTime",minWidth: 100,align: "center"},
                    {title: "到账银行",key: "receivingBank",minWidth: 150,align: "center",slot: "receivingBank"},
                    {title: "到账银行账户",key: "bankAccount",minWidth: 150,align: "center"},
                    {title: "付款客户",key: "paymentCust",minWidth: 100,align: "center"},
                    {title: "客户账号",key: "custAccount",minWidth: 150,align: "center"},
                    {title: "到账金额",key: "receivedAmt",minWidth: 100,align: "center"},
                    {title: "摘要",key: "summary",minWidth: 150,align: "center"},
                ],
                bankArriveList:[],
            }
        },
        mounted() {
            this.init();
        },
        methods: {
            saveHandle(){
                this.$Spin.show();
                //数据组装
                this.caseInfo.contractNo = this.contractInfo.contractNo;
                this.caseInfo.applyNo = this.contractInfo.applyNo;
                this.caseInfo.custName = this.contractInfo.custName;
                this.caseInfo.certNo = this.contractInfo.certNo;
                this.caseInfo.createTagStr = this.userInfo.userRealName;
                this.caseInfo.dataSource = this.obj.dataSource;
                //提前还款相关信息
                if(this.obj.caseType == 'BEFORE_CHARGE'){
                    this.caseInfo.debitOrderStatus = this.prepaymentInfo.debitOrderStatus;
                    if (!this.caseInfo.prepaymentApplyId){
                        this.caseInfo.prepaymentApplyId = this.obj.prepaymentApplyId;
                    }
                }
                //逾期天数
                this.caseInfo.overdueDays = this.contractInfo.overdueDaysHis;
                saveCorporate(this.caseInfo).then(res => {
                    if (res.code === "0000") {
                        this.caseInfo = res.data;
                        this.$Message.success("保存成功");
                    }
                    this.$Spin.hide();
                });
            },
            checkFile(){
                let params = {
                    busiNo:this.caseInfo.caseNo,
                    busiType: 'corporateTransfer',
                }
                checkFile(params).then(res => {
                    if (res.code === "0000") {
                        return true;
                    }else{
                        this.$Message.error("请上传打款凭证");
                        return false;
                    }
                }).catch(e => {
                    return false;
                });
            },
            submitHandle(){
                let params = {
                    busiNo:this.caseInfo.caseNo,
                    busiType: 'corporateTransfer',
                }
                checkFile(params).then(res => {
                    if (res.code === "0000") {
                        this.$refs.caseInfo.validate(valid => {
                            if (valid) {
                                this.$Spin.show();
                                //数据组装
                                this.caseInfo.contractNo = this.contractInfo.contractNo;
                                this.caseInfo.applyNo = this.contractInfo.applyNo;
                                this.caseInfo.custName = this.contractInfo.custName;
                                this.caseInfo.certNo = this.contractInfo.certNo;
                                this.caseInfo.createTagStr = this.userInfo.userRealName;
                                this.caseInfo.dataSource = this.obj.dataSource;
                                //提前还款相关信息
                                if(this.obj.caseType == 'BEFORE_CHARGE'){
                                    this.caseInfo.debitOrderStatus = this.prepaymentInfo.debitOrderStatus;
                                    if (!this.caseInfo.prepaymentApplyId){
                                        this.caseInfo.prepaymentApplyId = this.obj.prepaymentApplyId;
                                    }
                                }
                                //逾期天数
                                this.caseInfo.overdueDays = this.contractInfo.overdueDaysHis;
                                submitCorporate(this.caseInfo).then(res => {
                                    this.$Spin.hide();
                                    if (res.code === "0000") {
                                        this.$Message.success("提交成功");
                                        setTimeout(() => {
                                            this.$emit('afterHandle');
                                        }, 1000);
                                    }
                                }).catch(e => {
                                    this.$Spin.hide();
                                    return false;
                                });
                            }
                        });
                    }
                })
                // if (!this.checkFile()){
                //     return false;
                // }
            },
            initDataDic(){
                getDictDataList(this.dicKeys).then(res => {
                    this.dataDict = res;
                });
            },
            queryCaseInfo(){

                queryCorporateByCaseNo(this.obj.caseNo).then(res => {
                    if (res.code === "0000") {
                        this.caseInfo = res.data;
                        if (!this.caseInfo.caseSource || this.caseInfo.caseSource === ""){
                            this.caseInfo.caseSource = "BUSINESS_PEOPLE";
                        }
                        if (!this.caseInfo.caseType || this.caseInfo.caseType === ""){
                            this.caseInfo.caseType = this.obj.caseType;
                        }
                        if (!this.caseInfo.prepaymentApplyId || this.caseInfo.prepaymentApplyId === ""){
                            this.caseInfo.prepaymentApplyId = this.obj.prepaymentApplyId;
                        }
                        //加载提前还款信息
                        if("BEFORE_CHARGE" == this.obj.caseType){
                            this.queryPrepaymentInfo();
                        }
                    }
                });
            },
            queryContractInfo(){
                contractInfoForCorporate({contractNo:this.obj.contractNo}).then(res => {
                    if (res.code === "0000") {
                        this.contractInfo = res.data;
                    }
                });
            },
            queryPrepaymentInfo(){
                if (!this.caseInfo.prepaymentApplyId){
                    this.caseInfo.prepaymentApplyId = this.obj.prepaymentApplyId;
                }
                getApplyInfoById({id:this.caseInfo.prepaymentApplyId}).then(res => {
                    if (res.code === "0000") {
                        this.prepaymentInfo = res.data;
                    }
                });
            },
            openUploadModal() {
                //数据组装
                this.caseInfo.contractNo = this.contractInfo.contractNo;
                this.caseInfo.applyNo = this.contractInfo.applyNo;
                this.caseInfo.custName = this.contractInfo.custName;
                this.caseInfo.certNo = this.contractInfo.certNo;
                //逾期天数
                this.caseInfo.overdueDays = this.contractInfo.overdueDaysHis;
                saveCorporate(this.caseInfo).then(res => {
                    if (res.code === "0000") {
                        this.caseInfo = res.data;
                        this.fileParams = {
                            belongNo: this.caseInfo.contractNo,
                            busiNo:this.caseInfo.caseNo,
                            busiType: 'corporateTransfer',
                            busiData: {},
                            busiNodes:'corporateTransfer' //业务节点
                        }
                        this.isUploadFileModalOpen = true;
                    }else{
                        this.$Message.error("自动保存失败，请手动保存后再上传文件");
                    }
                }).catch(()=>{
                    this.$Message.error("自动保存失败，请手动保存后再上传文件");
                });
            },
            closeModal() {
                // 获取文件长度
                let l = this.$refs.uploadFileModal.getFileList()
                console.log(' 文件长度为：', l);

                this.isUploadFileModalOpen = false;
            },
            asyncSaveFileFn(data) {
                console.log('上传成功',data);
                this.$refs.formData.validate(valid => {
                    if (valid) {
                        let d = Object.assign({}, data, this.formData, this.oldData, this.Obj);

                        this.$emit(this.parentFuncName, d)
                        // this.$emit(funcName, d);
                    } else {
                        this.$Message.error("验证不通过!");
                        this.$emit("closeSpin");
                        verifyErrorToView(this.$refs.formData.fields);
                    }
                })
            },
            init(){
                //加载提前还款信息
                if("BEFORE_CHARGE" == this.obj.caseType){
                    this.showPanel = [2,3,4,5];
                }else{
                    this.showPanel = [2,3,5]
                }
                //加载数字字典
                this.initDataDic();
                //加载合同信息
                this.queryContractInfo();
                //操作历史记录数据加载
                this.approveHistoryObj.caseNo = this.obj.caseNo;
                this.approveHistoryObj.contractNo = this.obj.contractNo;
                //加载对公结算案件详情
                this.queryCaseInfo();
            },
        }
    }
</script>

<style scoped>

</style>
